//
//  MarketViewController.m
//  FreshBee
//
//  Created by Goofy on 15/10/2016.
//  Copyright © 2016 iOS013. All rights reserved.
//

#import "MarketViewController.h"
#import "YMTableViewCell.h"
#import "GoodsModel.h"
#import "MenuModel.h"

@interface MarketViewController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) NSArray<MenuModel *> *menuModelArr;

/**
 右边详情tableView
 */
@property (nonatomic,strong) UITableView *menuTableView;

/**
 左边菜单tableView
 */
@property (nonatomic,strong) UITableView *detailTableView;

@end

@implementation MarketViewController

#pragma mark - 控件懒加载

- (UITableView *)menuTableView
{
    if (!_menuTableView) {
        _menuTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width * 0.25, [UIScreen mainScreen].bounds.size.height)];
        
        _menuTableView.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1];
        _menuTableView.tableFooterView = [[UIView alloc] init];
        
        [_menuTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"menuCell"];
        
        _menuTableView.dataSource = self;
        _menuTableView.delegate = self;
        
    }
    return _menuTableView;
}

- (UITableView *)detailTableView
{
    if (!_detailTableView) {
        _detailTableView = [[UITableView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width * 0.25, 0, [UIScreen mainScreen].bounds.size.width * 0.75, [UIScreen mainScreen].bounds.size.height)];
        
        _detailTableView.rowHeight = 100;
        _detailTableView.sectionHeaderHeight = 20;
        
        [_detailTableView registerNib:[UINib nibWithNibName:@"YMTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        
        _detailTableView.dataSource = self;
        _detailTableView.delegate = self;
        
    }
    return _detailTableView;
}

- (void)setMenuModelArr:(NSArray<MenuModel *> *)menuModelArr
{
    _menuModelArr = menuModelArr;
    [self.menuTableView reloadData];
}

#pragma mark - 程序入口

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    [GoodsModel modelWithSuccess:^(NSArray<MenuModel *> *modelArr) {
        self.menuModelArr = modelArr;
    } error:^{
        
    }];
}

/**
 设置界面，加载视图
 */
- (void)setupUI {
    //设置背景颜色
    self.view.backgroundColor = [UIColor whiteColor];
    //设置tableView下移
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    //加载tableView
    [self setupTableView];
    
    
}

/**
 加载tableView
 */
- (void)setupTableView {
    
    [self.view addSubview:self.menuTableView];
    [self.view addSubview:self.detailTableView];
    
}

#pragma mark - tableView dataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == self.detailTableView) {
        return 3;
    } else {
        return 1;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.detailTableView) {
        return 3;
    } else {
        return self.menuModelArr.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.detailTableView) {
        YMTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        return cell;
    } else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"menuCell" forIndexPath:indexPath];
        
        cell.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1];
        cell.textLabel.text = [self.menuModelArr[indexPath.row] name];
        cell.textLabel.textColor = [UIColor darkGrayColor];
        
        return cell;
    }

}

#pragma mark - tableView delegate

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (tableView == self.detailTableView) {
        
        UILabel *label = [[UILabel alloc] init];
        label.backgroundColor = [UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:0.7];
        label.text = @"haha";
        return label;
    }
    return nil;
}


@end
